import edu.stanford.nlp.ling.Sentence;
import edu.stanford.nlp.ling.TaggedWord;
import java.io.*;
import java.util.*;
import net.siit.datatype.AnnotatedRange;
import net.siit.datatype.Range;
import net.siit.ling.TCCSeg;
import net.siit.ling.ThaiSeg;
import net.siit.ling.POSTagger;

/**
 *
 * @author arthit
 */
public class ThaiSegDemo {

    /**
     * @param args the command line arguments
     * @throws java.io.FileNotFoundException
     * @throws java.io.UnsupportedEncodingException
     * @throws java.io.IOException
     * @throws java.lang.Exception
     */
    public static void main(String[] args) throws UnsupportedEncodingException, IOException, Exception {
        long startTime, stopTime;
        String text = args[0];
        System.out.println("Input: [" + text + "]\n");

        startTime = System.currentTimeMillis();
        ThaiSeg seg = new ThaiSeg();
        TCCSeg tccSeg = new TCCSeg();
        POSTagger tagger = null;
        try {
            tagger = new POSTagger();
        } catch (Exception ex) {
            ex.printStackTrace();
            System.err.println("Can't create tagger.");
            System.exit(1);
        }
        stopTime = System.currentTimeMillis();
        System.out.println("Init time (ThaiSeg, TCCSeg, POSTagger): " + (stopTime - startTime) / 1000.0);
        System.out.println();

        startTime = System.currentTimeMillis();
        List<AnnotatedRange<Integer>> clusters = tccSeg.clusterize(text);
        stopTime = System.currentTimeMillis();
        System.out.println("Clusterize Text L->R: " + clusters.size() + " clusters; Time: " + (stopTime - startTime) / 1000.0);
        for (AnnotatedRange<Integer> cluster : clusters) {
            System.out.print(text.substring(cluster.getStart(),cluster.getEnd()));
            System.out.print("/" + cluster.getLabel() + ";");
        }
        System.out.println();

        startTime = System.currentTimeMillis();
        List<AnnotatedRange<Integer>> tokensTCC = seg.tokenizeLongestMatch(clusters, text);
        stopTime = System.currentTimeMillis();
        System.out.println("\nTokenized TCC L->R " + tokensTCC.size() + " tokens; Time: " + (stopTime - startTime) / 1000.0);
        printTokens(tokensTCC, text);
//        System.out.println(); System.out.println(tokensTCC);
        System.out.println();

        startTime = System.currentTimeMillis();
        List<AnnotatedRange<Integer>> tokensLR = seg.tokenizeLongestMatch(text);
        stopTime = System.currentTimeMillis();
        System.out.println("\nTokenized Text L->R " + tokensLR.size() + " tokens; Time: " + (stopTime - startTime) / 1000.0);
        printTokens(tokensLR, text);
//        System.out.println(); System.out.println(tokensLR);
        System.out.println();

        startTime = System.currentTimeMillis();
        List<AnnotatedRange<Integer>> tokensRL = seg.tokenizeLongestMatchRL(text);
        stopTime = System.currentTimeMillis();
        System.out.println("\nTokenized Text L<-R " + tokensRL.size() + " tokens (HAS BUG); Time: " + (stopTime - startTime) / 1000.0);
        printTokens(tokensRL, text);
//        System.out.println(); System.out.println(tokensRL);
        System.out.println();

        startTime = System.currentTimeMillis();
        Sentence ts = tagger.tag(text);
        stopTime = System.currentTimeMillis();
        System.out.println("\nTagged Text L->R " + ts.length() + " tokens; Time: " + (stopTime - startTime) / 1000.0);
        for (Object word : ts) {
            TaggedWord tw = (TaggedWord) word;
            System.out.print(tw.word() + "/" + tw.tag() + " ");
        }
        System.out.println();
    }

    public static void printTokens(List<AnnotatedRange<Integer>> ranges, String text) {
        for (AnnotatedRange<Integer> range : ranges) {
            System.out.print(text.substring(range.getStart(), range.getEnd()));
            System.out.print("/");
            switch (range.getLabel()) {
                case ThaiSeg.TOKEN_LABEL_THAI_DICTIONARY_WORD:
                    System.out.print("w");
                    break;
                case ThaiSeg.TOKEN_LABEL_THAI_NON_DICTIONARY_WORD:
                    System.out.print("nw");
                    break;
                case ThaiSeg.TOKEN_LABEL_PUNCTUATION:
                    System.out.print("p");
                    break;
                default:
                    System.out.print(range.getLabel());
                    break;
            }
            System.out.print(" ");
        }
    }
}
